package com.app.controller;


import com.app.common.Result;
import com.app.model.GoodsSpecsModel;
import com.app.model.UserModel;
import com.app.service.AddressService;
import com.app.service.GoodsSpecsService;
import com.app.service.UserService;
import com.app.utils.CommonHelp;
import com.app.utils.TokenUtils;
import com.app.vo.GoodsSpecsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static cn.hutool.crypto.digest.DigestUtil.md5Hex;

/**
 * 商品规格表
 *
 * @author 16693103420
 * @since 2024-01-25
 */
@RestController
@RequestMapping("/goodsSpecs")
public class GoodsSpecsController {
    @Autowired
    private GoodsSpecsService goodsSpecsService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressService addressService;
    // 查询列表
    @GetMapping("/getGoodsSpecsList")
    public Result getGoodsSpecsList(@RequestParam(defaultValue = "0") Integer page,
                                    @RequestParam(defaultValue = "10") Integer limit, Integer goodsId) {
        GoodsSpecsVo GoodsSpecsVo = goodsSpecsService.getGoodsSpecsList(page, limit, goodsId);
        return Result.success(GoodsSpecsVo);
    }

    //兑换商品
    @PostMapping("/exchangeGoods")
    public Result exchangeGoods(@RequestBody GoodsSpecsModel goodsSpecsModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        if (goodsSpecsModel.getAddressId() == null) {
            return Result.error(201, "请选择收货地址");
        }
        if (userModel.getPaymentPassword() == null) {
            return Result.error(201, "请设置支付密码");
        }
        if (goodsSpecsModel.getPassword() == null) {
            return Result.error(405, "请输入支付密码");
        }
        String newWord = md5Hex(goodsSpecsModel.getPassword() + userModel.getSalt());
        if (!userModel.getPaymentPassword().equals(newWord)) {
            return Result.error(201, "支付密码错误");
        }
        String message = goodsSpecsService.exchangeGoods(goodsSpecsModel, userModel.getId());
        if ("兑换成功".equals(message)) {
            return Result.success(message);
        } else {
            return Result.error(201, message);
        }
    }

    //根据id查询
    @GetMapping("/getGoodsSpecsById")
    public Result getGoodsSpecsById(Integer id) {
        GoodsSpecsModel GoodsSpecs = goodsSpecsService.getById(id);
        return Result.success(GoodsSpecs);
    }

    //新增和修改
    @PostMapping("/addAndUpdateGoodsSpecs")
    public Result addAndUpdateGoodsSpecs(@RequestBody GoodsSpecsModel GoodsSpecsModel) {
        GoodsSpecsModel GoodsSpecs = goodsSpecsService.getById(GoodsSpecsModel.getId());
        if (GoodsSpecs != null) {
            GoodsSpecsModel.setUpdateTime(CommonHelp.getNowTimestamp());
            goodsSpecsService.updateById(GoodsSpecsModel);
            return Result.success("修改成功");
        } else {
            GoodsSpecsModel.setCreateTime(CommonHelp.getNowTimestamp());
            goodsSpecsService.save(GoodsSpecsModel);
            return Result.success("添加成功");
        }
    }

    //根据id删除
    @GetMapping("/delGoodsSpecs")
    public Result delGoodsSpecs(Integer id) {
        goodsSpecsService.removeById(id);
        return Result.success("删除成功");
    }
}

